package cn.cms.service;

import cn.cms.dao.SysMenuDao;
import cn.cms.model.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    public List<SysMenu> getMenus(Collection<String> menuIds) {
        List<SysMenu> navis = sysMenuDao.getNavi();
        navis.forEach(k -> {
            k.setText(k.getMenuName());
            if (menuIds.contains(String.valueOf(k.getId()))) {
                Map<String, Boolean> temp = new HashMap<>();
                temp.put("checked", true);
                k.setState(temp);
            }
            getByPid(k, menuIds);
        });
        return navis;
    }

    public List<SysMenu> getMenus(Collection<String> menuIds, Collection<String> pMenuIds) {
        List<SysMenu> navis = sysMenuDao.getNavi();
        navis.forEach(k -> {
            k.setText(k.getMenuName());
            if (menuIds.contains(String.valueOf(k.getId()))) {
                Map<String, Boolean> temp = new HashMap<>();
                temp.put("checked", true);
                k.setState(temp);
            }
            if (pMenuIds != null && !pMenuIds.contains(String.valueOf(k.getId()))) {
                k.setSelectable(false);
                List<String> tags = new ArrayList<>();
                tags.add("*父级无此菜单");
                k.setTags(tags);
            } else {
                k.setSelectable(true);
            }
            getByPid(k, menuIds, pMenuIds);
        });
        return navis;
    }

    public String getCheckedMenuIds(List<SysMenu> sysMenus) {
        Set<String> menuIds = new HashSet<>();
        getCheckedMenuIds(sysMenus, menuIds);
        StringBuilder sb = new StringBuilder();
        menuIds.forEach(k -> {
            sb.append(k).append(",");
        });
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - 1);
        }
        return "";
    }

    private void getCheckedMenuIds(List<SysMenu> sysMenus, Set<String> menuIds) {
        sysMenus.forEach(k -> {
            Map<String, Boolean> temp = k.getState();
            if (temp != null && temp.get("checked") != null && k.getState().get("checked") == true
                    && (k.getSelectable() == null ? true : k.getSelectable())) {
                menuIds.add(String.valueOf(k.getId()));
            }
            if (k.getNodes() != null && k.getNodes().size() > 0) {
                getCheckedMenuIds(k.getNodes(), menuIds);
            }
        });
    }

    public List<SysMenu> getMenus() {
        List<SysMenu> navis = sysMenuDao.getNavi();
        navis.forEach(k -> {
            k.setText(k.getMenuName());
            getByPid(k);
        });
        return navis;
    }

    public int getMaxSort(int pid) {
        return sysMenuDao.getMaxSort(pid);
    }

    public SysMenu getMenuById(int id) {
        return sysMenuDao.get(id);
    }

    public void delete(SysMenu sysMenu) {
        sysMenuDao.delete(sysMenu);
    }

    public void update(SysMenu sysMenu) {
        sysMenuDao.update(sysMenu);
    }

    public void save(SysMenu sysMenu) {
        sysMenuDao.save(sysMenu);
    }

    /**
     * 逐级获取菜单节点
     *
     * @param sysMenu
     */
    private void getByPid(SysMenu sysMenu) {
        List<SysMenu> sysMenus = sysMenuDao.getByPid(sysMenu.getId());
        if (sysMenus != null && sysMenus.size() > 0) {
            sysMenus.forEach(k -> {
                k.setText(k.getMenuName());
                getByPid(k);
            });
            sysMenu.setNodes(sysMenus);
        }
    }

    /**
     * 逐级获取菜单节点
     *
     * @param sysMenu
     */
    private void getByPid(SysMenu sysMenu, Collection<String> menuIds) {
        List<SysMenu> sysMenus = sysMenuDao.getByPid(sysMenu.getId());
        if (sysMenus != null && sysMenus.size() > 0) {
            sysMenus.forEach(k -> {
                k.setText(k.getMenuName());
                if (menuIds.contains(String.valueOf(k.getId()))) {
                    Map<String, Boolean> temp = new HashMap<>();
                    temp.put("checked", true);
                    k.setState(temp);
                }
                getByPid(k, menuIds);
            });
            sysMenu.setNodes(sysMenus);
        }
    }

    /**
     * 逐级获取菜单节点
     *
     * @param sysMenu
     */
    private void getByPid(SysMenu sysMenu, Collection<String> menuIds, Collection<String> pMenuIds) {
        List<SysMenu> sysMenus = sysMenuDao.getByPid(sysMenu.getId());
        if (sysMenus != null && sysMenus.size() > 0) {
            sysMenus.forEach(k -> {
                k.setText(k.getMenuName());
                if (menuIds.contains(String.valueOf(k.getId()))) {
                    Map<String, Boolean> temp = new HashMap<>();
                    temp.put("checked", true);
                    k.setState(temp);
                }
                if (pMenuIds != null && !pMenuIds.contains(String.valueOf(k.getId()))) {
                    k.setSelectable(false);
                    List<String> tags = new ArrayList<>();
                    tags.add("父级无此菜单");
                    k.setTags(tags);
                } else {
                    k.setSelectable(true);
                }
                getByPid(k, menuIds, pMenuIds);
            });
            sysMenu.setNodes(sysMenus);
        }
    }

}
